//
//  HMVisitorView.swift
//  新浪微博--song
//
//  Created by 宋海保 on 16/4/8.
//  Copyright © 2016年 宋海保. All rights reserved.
//

import UIKit


//1.定义代理协议
protocol HMVisitorViewDelegate: NSObjectProtocol {
    //2.定义代理方法
    func visitorViewDidClickRegisterButton()
    func visitorViewDidClickLoginButton()
}

class HMVisitorView: UIView {
    
    
    //3.定义代理属性
    weak var delegate: HMVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 公开方法
    func setupVisitorViewInfo(imageName: String, message: String) {
        
        
        //设置转轮的图片
        iconView.image = UIImage(named: imageName)
        //设置消息内容
        messageLabel.text = message
        //隐藏小房子
        HomeView.hidden = true
        
        //隐藏遮盖
//        coverView.hidden = true
        //将一个view弄到最底部
        sendSubviewToBack(coverView)
        
        //将一个view弄到最前面
//        bringSubviewToFront(coverView)
        
        
        
    }
    
    //MARK - 公开方法
    //转轮动画
    func startRotation() {
        
        //使用核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        //动画完成后不要移除动画
        anim.removedOnCompletion = false
        
        //添加动画到iconView
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    //MARK - 按钮点击事件
    
    //点击注册按钮
    @objc private func didClickRegisterButton() {
        //4.调用代理协议方法
        delegate?.visitorViewDidClickRegisterButton()
    }
    
    @objc private func didClickLoginButton() {
        delegate?.visitorViewDidClickLoginButton()
    }
    
    
    //准备UI
    private func setupUI() {
        //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(HomeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置约束
        
        
        //禁用translatesAutoresizingMaskIntoConstraints
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        HomeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -40))
        
        //遮盖
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Bottom, relatedBy: .Equal, toItem: registerButton, attribute: .Bottom, multiplier: 1, constant: 0))
        
        
        //小房子
        addConstraint(NSLayoutConstraint(item: HomeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: HomeView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //消息标签
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 250))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录按钮
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: registerButton, attribute: .Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: registerButton, attribute: .Height, multiplier: 1, constant: 0))
        
        
        
    }
    
    //MARK: - 懒加载
    //转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var HomeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息文字
    private lazy var messageLabel: UILabel =  {
        let label = UILabel()
        
        label.text = "关注一些人,看看有什么惊喜!"
        label.font = UIFont.systemFontOfSize(17)
        label.textAlignment = .Center
        label.numberOfLines = 0
        return label
    }()
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.addTarget(self, action: Selector("didClickRegisterButton"), forControlEvents: .TouchUpInside)
        return button
    }()
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable") , forState: .Normal)
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        button.addTarget(self, action: Selector("didClickLoginButton"), forControlEvents: .TouchUpInside)
        return button
    }()
}
